# @author: uuz
# encoding = utf-8

from flask import Blueprint, jsonify
from flask_restful import Api, Resource, reqparse, abort

from ext import db
from models import User

customer_bp = Blueprint("customer", __name__, url_prefix='/api/user')
api = Api(customer_bp)


class RegisterView(Resource):
    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('account', type=str, required=True)
        self.parser.add_argument('nickname', type=str, required=True)
        self.parser.add_argument('realname', type=str, required=True)
        self.parser.add_argument('password', type=str, required=True)
        self.parser.add_argument('mobile', type=str, required=True)

    def post(self):
        args = self.parser.parse_args()
        user_account = args.get('account')
        user_nickname = args.get('nickname')
        realname = args.get('realname')
        pwd = args.get('password')
        mobile = args.get('mobile')

        if User.query.filter(User.account == user_account).first():
            abort(400, message="该用户已存在")
        else:
            user = User(user_account, pwd, realname, nickname=user_nickname, mobile=mobile)
            db.session.add(user)
            db.session.commit()
            return jsonify({"result": "OK"})


api.add_resource(RegisterView, '/register/')


class LoginView(Resource):
    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('account', type=str, required=True)
        self.parser.add_argument('password', type=str, required=True)

    def post(self):
        args = self.parser.parse_args()
        account = args.get('account')
        password = args.get('password')
        user = User.query.filter(User.account == account).first()
        if user:
            if user.verify_password(password):
                return jsonify({
                    'result': "OK",
                    'account': user.account,
                    'user_id': user.id})
            else:
                abort(400, message='密码错误')
        else:
            abort(400, message='账号不存在')


api.add_resource(LoginView, '/login/')


class UpdateUserInfoView(Resource):
    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('account', type=str, required=True)
        self.parser.add_argument('nickname', type=str, required=False)
        self.parser.add_argument('password', type=str, required=False)
        self.parser.add_argument('mobile', type=str, required=False)

    def post(self):
        args = self.parser.parse_args()
        account = args.get('account')
        nickname = args.get('nickname')
        password = args.get('password')
        mobile = args.get('mobile')

        user = User.query.filter(User.account == account).first()

        if user:
            if account:
                user.account = account
            if nickname:
                user.nickname = nickname
            if password:
                user.password = password
            if mobile:
                if len(mobile) > 11:
                    abort(400, message="手机号格式错误")
                else:
                    user.mobile = mobile
            db.session.commit()
            return jsonify({"result": "修改成功"})
        else:
            abort(400, message="用户不存在")


api.add_resource(UpdateUserInfoView, '/update/')


class GetUserIdView(Resource):
    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('account', type=str, required=True)

    def get(self):
        account = self.parser.parse_args().get('account')
        user = User.query.filter(User.account == account).first()
        if user:
            return jsonify({"user_id": user.id})
        else:
            abort(400, message="用户不存在")


api.add_resource(GetUserIdView, '/QueryUserId/')


class GetUserInfoView(Resource):
    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('user_id', type=int, required=True)

    def get(self):
        user_id = self.parser.parse_args().get('user_id')
        user = User.query.filter(User.id == user_id).first()
        if user:
            return jsonify({
                "account": user.account,
                "nickname": user.nickname,
                "realname": user.realname,
                "mobile": user.mobile
            })
        else:
            abort(400, message="用户不存在")


api.add_resource(GetUserInfoView, '/QueryUserInfo/')
